
#import "skinView.h"

#import <SDWebImage/SDWebImageManager.h>
#import "TF_inviteView.h"
@implementation skinModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.avatar = @"";
        self.qrcodeUrl = @"";
        self.company = @"";
        self.nickname = @"";
        self.descriptionMsg = @"";
        self.startingTime = @"";
        self.title = @"";
        self.isShowNavigationBar = true;
    }
    return self;
}
@end


@implementation fingerprintBtn

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
        
        [self setTitleColor:[UIColor colorWithRed:103/255.0 green:123/255.0 blue:139/255.0 alpha:1.0] forState:UIControlStateNormal];
        self.backgroundColor = [UIColor clearColor];
        
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        UIImage *newImage = [TF_inviteView getImage:@"指纹"];
        [self setImage:newImage forState:UIControlStateNormal];
        NSString *fingerprintMsg = @"长按扫码看视频";
        [self setTitle:fingerprintMsg forState:UIControlStateNormal];
        
    NSDictionary *fingerprint_attrs = @{NSFontAttributeName : self.titleLabel.font };
    CGSize fingerprint_attrsSize = [fingerprintMsg sizeWithAttributes:fingerprint_attrs];
    
        self.strWidth = fingerprint_attrsSize.width;
    }
   
    return self;
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.frame = CGRectMake((self.frame.size.width-78)/2, 0, 78, 70);
    CGFloat titleLabelH = 13;
    CGFloat titleLabelY = self.frame.size.height-titleLabelH;
    
    self.titleLabel.frame = CGRectMake(0, titleLabelY, self.frame.size.width, titleLabelH);
}
@end
//----------------------------------------------------------------------
@interface skinView ()

@property(nonatomic,strong)UIImageView*view;

@property(nonatomic,strong)NSURLSession *session;
/**用户头像*/
@property(nonatomic,strong)UIImageView *profilePicture;
//邀请人的名字
@property(nonatomic,strong)UILabel *nameLabel;
//
@property(nonatomic,strong)UILabel *titleLabel_2;
//标语
@property(nonatomic,strong)UILabel *sloganLabel;
//直播时间：8月25日 10:00
@property(nonatomic,strong)UILabel *startingTimeLabel;
//举办方
@property(nonatomic,strong)UILabel *companylabel;
//二维码
@property(nonatomic,strong)UIImageView *QRCode;
//指纹
@property(nonatomic,strong)fingerprintBtn *fingerprint;
//虚线
@property(nonatomic,strong)CAShapeLayer *shapeLayer;
@property(nonatomic,strong)SDWebImageManager *imageManage;

@end

@implementation skinView

+ (skinView*)shared
{
    static skinView *s_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_instance = [[self alloc] init];
       
        s_instance.session = [[NSURLSession alloc]init];
    });
    return s_instance;
}


+ (void)options:(NSInteger)state
{
    UIImage *newImage = [TF_inviteView getImage:[NSString stringWithFormat:@"skin_%li",(long)state]];

    [skinView shared].view.image = newImage;
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.view = [[UIImageView alloc]init];
        self.view.backgroundColor = [UIColor clearColor];
        self.view.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.view];
        
        CGFloat skinW = 375.0;
        CGFloat skinH = 667.0;
        self.view.frame = CGRectMake(0, -9999, skinW, skinH);

        //--------------------------
        //用户头像
        self.profilePicture = [[UIImageView alloc]init];
        self.profilePicture.backgroundColor = [UIColor clearColor];
        self.profilePicture.contentMode = UIViewContentModeScaleAspectFill;
        [self.view addSubview:self.profilePicture];
        self.profilePicture.layer.masksToBounds = true;
        //0---------------
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.textAlignment = NSTextAlignmentCenter;
       
        [self.view addSubview:self.nameLabel];
        self.nameLabel.font = [UIFont systemFontOfSize:16];
        self.nameLabel.textColor = [UIColor colorWithRed:15/255.0 green:38/255.0 blue:60/255.0 alpha:1.0];
        //------------------
        self.titleLabel_2 = [[UILabel alloc] init];
        self.titleLabel_2.numberOfLines = 0;
        [self.view addSubview:self.titleLabel_2];
       
        self.titleLabel_2.textColor = [UIColor colorWithRed:103/255.0 green:123/255.0 blue:139/255.0 alpha:1.0];
        self.titleLabel_2.font = [UIFont fontWithName:@"PingFang SC" size: 16];
        self.titleLabel_2.textAlignment = NSTextAlignmentCenter;
        //------------------------------
        self.sloganLabel = [[UILabel alloc] init];
        self.sloganLabel.numberOfLines = 0;
        [self.view addSubview:self.sloganLabel];
        self.sloganLabel.textAlignment = NSTextAlignmentCenter;
        self.sloganLabel.backgroundColor = [UIColor clearColor];
        

      //-----------------
        self.startingTimeLabel = [[UILabel alloc] init];
        self.startingTimeLabel.textAlignment = NSTextAlignmentLeft;
        self.startingTimeLabel.numberOfLines = 0;
        [self.view addSubview:self.startingTimeLabel];
        

        //---------
        self.companylabel = [[UILabel alloc] init];
        self.companylabel.textAlignment = NSTextAlignmentLeft;
        self.companylabel.numberOfLines = 0;
        [self.view addSubview:self.companylabel];
        

        //-------------------
        self.QRCode = [[UIImageView alloc]init];
        self.QRCode.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.QRCode];
        //----------
        self.fingerprint = [[fingerprintBtn alloc]init];
        [self.view addSubview:self.fingerprint];
        
        

    
    }
    return self;
}
/**
 * 当前控件的frame发生改变的时候就会调用
 * 这个方法专门用来布局子控件，设置子控件的frame
 */
- (void)layoutSubviews
{
    // 一定要调用super方法
    [super layoutSubviews];
    


//--------------------------------------



   
}


+(NSString *)UTCchangeDate:(NSString *)utc{

NSTimeInterval time = [utc doubleValue];

NSDate *date=[NSDate dateWithTimeIntervalSince1970:time];

NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];

[dateformatter setDateFormat:@"MM月dd日 HH:mm"];

NSString *staartstr=[dateformatter stringFromDate:date];

return staartstr;

}

+ (void)getCurrent:(skinModel*)model callback:(void (^_Nonnull)( UIImage *_Nonnull image))callback{
    //-------------------
    
    NSMutableAttributedString * titleLabel_string = [[NSMutableAttributedString alloc]initWithString:@"邀请您来一起看直播" attributes:@{NSFontAttributeName:[skinView shared].titleLabel_2.font,NSForegroundColorAttributeName:[UIColor colorWithRed:103/255.0 green:123/255.0 blue:139/255.0 alpha:1.0]}];
    [skinView shared].titleLabel_2.backgroundColor = [UIColor clearColor];
    [skinView shared].titleLabel_2.attributedText = titleLabel_string;
    
    //--------------------
    
    [skinView shared].nameLabel.text = model.nickname;
    //------------
    
    NSString *startingTimeMsg = @"直播时间：";
    
    NSMutableAttributedString * startingTimeTabel_string = [[NSMutableAttributedString alloc]initWithString:[startingTimeMsg stringByAppendingString:[self UTCchangeDate:model.startingTime]] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithRed:103/255.0 green:123/255.0 blue:139/255.0 alpha:1.0]}];
    [skinView shared].startingTimeLabel.attributedText = startingTimeTabel_string;
    
    //举办方
    NSString *companylabelMsg = @"主播：";

    NSMutableAttributedString * companylabel_string = [[NSMutableAttributedString alloc]initWithString:[companylabelMsg stringByAppendingString:model.company] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithRed:103/255.0 green:123/255.0 blue:139/255.0 alpha:1.0]}];
    [skinView shared].companylabel.attributedText = companylabel_string;
    
    
    //宣传口号
    NSMutableAttributedString * sloganLabel_string = [[NSMutableAttributedString alloc]initWithString:model.descriptionMsg attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithRed:15/255.0 green:38/255.0 blue:60/255.0 alpha:1.0]}];

    [skinView shared].sloganLabel.attributedText = sloganLabel_string;
    
    
//    __weak typeof(self) weakSelf = self;
    [[skinView shared] url:model.qrcodeUrl callback:^(NSInteger code, UIImage * _Nonnull qr_image) {
        if (code==0) {
            [skinView shared].QRCode.image = qr_image;
        }else
        {
            
        }
       
        
        [[skinView shared] url:model.avatar callback:^(NSInteger code2,UIImage * _Nonnull avatar_image) {
            if (code2==0) {
            ///**用户头像*/
            [skinView shared].profilePicture.image = avatar_image;
            }else
            {
                
            }
  
            if(callback)
            {
                callback([self currentViewScreenPhoto:model.state]);
            }
            
            
        }];
        
    }];
    
    
    [[skinView shared] layoutSubviewsUI];


}

- (void)layoutSubviewsUI
{
    CGFloat profilePictureW = 60.0;
    CGFloat profilePictureY = 101;
    self.profilePicture.frame = CGRectMake((self.view.bounds.size.width-profilePictureW)/2, profilePictureY, 60, 60);
    
    self.profilePicture.layer.cornerRadius = profilePictureW/2;
    //-------------
    self.nameLabel.frame = CGRectMake(0, self.profilePicture.frame.origin.y+self.profilePicture.frame.size.height+11, self.view.frame.size.width, 16);

    //--------------------
    CGFloat titleLabelX = 67;
    CGFloat titleLabelW = self.view.frame.size.width-titleLabelX-titleLabelX;
    CGFloat titleLabelH = [self sizeLabelToFit:self.titleLabel_2.attributedText width:titleLabelW fontSize:self.titleLabel_2.font].height;
    
   
    self.titleLabel_2.frame = CGRectMake(titleLabelX,self.nameLabel.frame.origin.y+self.nameLabel.frame.size.height+6.5,titleLabelW, titleLabelH);
    //-----------------------
    CGFloat sloganLabelX = 67;
    CGFloat sloganLabelW = self.view.frame.size.width-sloganLabelX-sloganLabelX;
    self.sloganLabel.frame = CGRectMake(sloganLabelX,self.titleLabel_2.frame.origin.y+self.titleLabel_2.frame.size.height+44,sloganLabelW,51);
    //-----------
    CGFloat startingTimeLabelX = 94;
    self.startingTimeLabel.frame = CGRectMake(startingTimeLabelX,self.sloganLabel.frame.origin.y+self.sloganLabel.frame.size.height+37,self.view.frame.size.width,15.5);
    //-------------
    self.companylabel.frame = CGRectMake(startingTimeLabelX, self.startingTimeLabel.frame.origin.y+self.startingTimeLabel.frame.size.height+12.5, self.view.frame.size.width, 15.5);
    
    //----------

    //--------------
    CGFloat QRCodeWidth = 90;
    CGFloat QRCodeHhight = 90;
    self.QRCode.frame = CGRectMake(83, self.companylabel.frame.origin.y+self.companylabel.frame.size.height+60.5, QRCodeWidth, QRCodeHhight);
    //---------------
    CGFloat fingerprintX = self.QRCode.frame.origin.x+self.QRCode.frame.size.width+29;
    self.fingerprint.frame = CGRectMake(fingerprintX,  self.QRCode.frame.origin.y, self.fingerprint.strWidth, QRCodeHhight);
}

/**
 *  返回UILabel自适应后的size
 *
 *  @param aString 字符串
 *  @param width   指定宽度
 *
 *  @return CGSize
 */
- (CGSize)sizeLabelToFit:(NSAttributedString *_Nonnull)aString width:(CGFloat)width fontSize:(UIFont*_Nullable)font{
    

    UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, 0)];
    tempLabel.frame = CGRectMake(0, 0, width, 0);
    tempLabel.font = font;
    tempLabel.attributedText = aString;
    tempLabel.numberOfLines = 0;

    [tempLabel sizeToFit];
    CGSize size = tempLabel.frame.size;
    tempLabel.lineBreakMode = NSLineBreakByCharWrapping;
    
    

    return size;
}

+ (UIImage *)currentViewScreenPhoto:(NSInteger)state
{
    [self options:state];
    
   return [self UIGraphicsBeginImageContextWithOptions];
}

+(UIImage*)UIGraphicsBeginImageContextWithOptions
{
    // 高清截屏
    UIGraphicsBeginImageContextWithOptions([skinView shared].view.bounds.size, NO, 0.0);
    [[skinView shared].view.layer renderInContext:UIGraphicsGetCurrentContext()];
    // 保存为图片
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}
-(SDWebImageManager *)imageManage
{
    if (_imageManage==nil) {
        _imageManage = [SDWebImageManager sharedManager ];
    }
    return _imageManage;
}
- (void)url:(NSString*)url callback:(void (^_Nonnull)(NSInteger code, UIImage *_Nonnull image))callback
{
    
if([url isKindOfClass:[NSString class]])
{
    [[skinView shared].imageManage loadImageWithURL:[NSURL URLWithString:url] options:32 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        // 回到主线程更新UI
        NSInteger new_code = 0;
        if (!error) {
            if(callback)
            {
                callback(new_code,image);
            }
        }else
        {
            if(callback)
            {
                callback(-1,image);
            }
        }
    }
 
     ];
}

   
}

@end
